//
//  PhoneVerifyViewController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/1/29.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "PhoneVerifyViewController.h"
#import "Common.h"
#import "PasswordChangeViewController.h"
#import "TransforController.h"

@interface PhoneVerifyViewController (){
    UITextField *_txtPhoneNumber;
    UITextField *_txtVerifyNumber;
    UIButton *btnGetVerifyNumber;
    NSString *verifyNumber;
    UIButton *btnConfirm;
}

@end

@implementation PhoneVerifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initView{
    
    UIView *view_navigationBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0];
    [self.view addSubview:view_navigationBar];
    
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"PhoneLoginView" owner:nil options:nil];
    self.view = [nibView objectAtIndex:0];
    
    _txtPhoneNumber = (UITextField *)[self.view viewWithTag:30001];
    btnGetVerifyNumber = (UIButton *)[self.view viewWithTag:30003];
    [btnGetVerifyNumber addTarget:self action:@selector(getVerifyNumberAction) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:btnGetVerifyNumber];
    
    _txtVerifyNumber = (UITextField *)[self.view viewWithTag:30002];
    btnConfirm = (UIButton *)[self.view viewWithTag:30004];
    [btnConfirm setTitle:@"验证并修改" forState:UIControlStateNormal];
    [btnConfirm addTarget:self action:@selector(doConfirmAction) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:btnConfirm];
}

- (void)getVerifyNumberAction{
    if([_txtPhoneNumber.text isEqualToString:@""] || [_txtPhoneNumber.text length] != 11){
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"请输入正确的手机号码！" dismissTime:1.2];
        return;
    }else{
        [[ProtocolManager shareInstance] getVerifyCodeWithMobile:_txtPhoneNumber.text target:self selector:@selector(mobileValidationCallback:)];
        [self startTime];
    }
}

- (void)mobileValidationCallback:(NSDictionary*)dic
{
    NSLog(@"mobileValidationCallback %@", dic)
    if (dic == nil) {
        return;
    }
    if([[Common valueForKey:@"respCode" dic:dic] isEqualToString:@"000"]){
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"短信发送成功" dismissTime:0.8];
        verifyNumber = [[NSString alloc]initWithFormat:@"%@",[Common valueForKey:P_CODE dic:dic]];
        [[Common shareInstance]stopLoadingView];
    }
}

- (void)doConfirmAction{
    NSString *tempString = _txtVerifyNumber.text;
    if([tempString isEqualToString:verifyNumber]){
        NSLog(@"%s || verifyNumber's correct!", __FUNCTION__);
        [self gotoPasswordChange];
    }else{
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"请校验验证码是否正确！" dismissTime:1.2];
        return;
    }
}

- (void)gotoPasswordChange{
    PasswordChangeViewController *instance_passwordChange = [[PasswordChangeViewController alloc]init];
    instance_passwordChange.hidesBottomBarWhenPushed = YES;
    instance_passwordChange.isChangeWithPhone = YES;
    instance_passwordChange.string_mobile = _txtPhoneNumber.text;
    [self.navigationController pushViewController:instance_passwordChange animated:YES];
}

-(void)startTime{
    __block int timeout=30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [btnGetVerifyNumber setTitle:@"获取验证码" forState:UIControlStateNormal];
                btnGetVerifyNumber.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                NSLog(@"____%@",strTime);
                [btnGetVerifyNumber setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                btnGetVerifyNumber.userInteractionEnabled = NO;
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
}
@end
